package com.mmall.common;

import java.io.*;

/**
 * Created by chengwei on 2018/5/22.
 */
public class TestIo {
        //按字节写入 FileOutputStream
        public void  outputStreamTest(int count ,String str){
            File f = new File("g:test.txt");
            OutputStream os = null;
            try {
                os = new FileOutputStream(f);
                for (int i =0;i<count;i++){
                    os.write(str.getBytes());
                }
                os.flush();
                System.out.println("file's long :"+f.length());
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    os.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }

        // 按字节缓冲写入 BufferedOutputStream
        public void bufferedOutputTest(int count,String str){
            File file = new File("");
            BufferedOutputStream bos = null;
            try {
                OutputStream os = new FileOutputStream(file);
                bos = new BufferedOutputStream(os);
                for (int i = 0;i<count;i++){
                    bos.write(str.getBytes());
                }
                bos.flush();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    bos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    public static void main(String[] args) {
        String str = "abcdefghiJKLMN！";
        int count = 1000000;
        TestIo t = new TestIo();

        //1.fileWrite's time
        long start = System.currentTimeMillis();
        t.outputStreamTest(count, str);
        long end = System.currentTimeMillis();
        System.out.println("fileWrite's time---------" + (start - end));
    }

}
